package com.factory.test.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityLcdBinding;
import com.factory.test.view.GrayscaleView;
import com.factory.test.view.MulticolorView;


/**
 * LCD测试
 */
public class LcdActivity extends BaseTestActivity {

    private ActivityLcdBinding binding;

    private boolean clickTestButton;

    private int[] colors = new int[]{
            Color.BLACK,
            Color.DKGRAY,
            Color.GRAY,
            Color.LTGRAY,
            Color.WHITE,
            Color.RED,
            Color.GREEN,
            Color.BLUE,
            Color.YELLOW,
            Color.CYAN,
            Color.MAGENTA,
            1,
            2,
            3,
            4,
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lcd);
        binding.setViewClick(new ViewClick());
        binding.view.setVisibility(View.GONE);

        brightness = sdkService.getBrightness();
    }

    private int index = 0;
    private int brightness = 50;
    public class ViewClick {
        public void onStart() {
            clickTestButton = true;
            sdkService.setBrightness(100);
            fullscreen(false);                    //全屏, 隐藏状态栏及导航栏
            binding.view.setVisibility(View.VISIBLE);
            binding.colorViewLayout.setVisibility(View.GONE);
            index = 0;
            binding.view.setBackgroundColor(colors[index++]);
        }

        public void onNext() {
            if (index >= colors.length) {
                binding.view.setVisibility(View.GONE);
                binding.colorViewLayout.setVisibility(View.GONE);
                index = 0;
                fullscreen(true);                   //显示, 退出全屏
                sdkService.setBrightness(brightness);    //将屏幕调到之前的亮度
            } else {
                int c = colors[index++];
                if(index  == 11){
                    binding.colorViewLayout.setVisibility(View.VISIBLE);
                    binding.colorViewLayout.removeAllViews();
                    View view = new GrayscaleView(context);
                    binding.colorViewLayout.addView(view);

                }else if(index == 12){
                    binding.colorViewLayout.setVisibility(View.VISIBLE);
                    binding.colorViewLayout.removeAllViews();
                    View view = new GrayscaleView(context, true);
                    binding.colorViewLayout.addView(view);

                }else if(index == 13){
                    binding.colorViewLayout.setVisibility(View.VISIBLE);
                    binding.colorViewLayout.removeAllViews();
                    View view = new MulticolorView(context);
                    binding.colorViewLayout.addView(view);

                }else if(index == 14){
                    binding.colorViewLayout.setVisibility(View.VISIBLE);
                    binding.colorViewLayout.removeAllViews();
                    View view = new MulticolorView(context, true);
                    binding.colorViewLayout.addView(view);
                }else {
                    binding.view.setBackgroundColor(c);
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (index == 0) {
            super.onBackPressed();
            return;
        }
        binding.getViewClick().onNext();
    }


    public void onOK(){
        if(!clickTestButton){
            showError("请先点击 开始全屏测试LCD");
            return;
        }

        super.onOK();
    }

    public void onNG(){
        super.onNG();
    }
}
